package com.pureut.production.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.production.domain.GroupManagementDetail;
import com.pureut.production.domain.dto.GroupManagementDetailDto;
import com.pureut.production.domain.dto.GroupManagementDetailViewDto;
import com.pureut.production.domain.vo.GroupManagementDetailVo;
import com.pureut.production.domain.vo.GroupVo;
import com.pureut.production.mapper.GroupManagementDetailMapper;
import com.pureut.production.service.GroupManagementDetailService;
import com.pureut.system.api.SchedulingPlanFeignService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: CM
 * @date: 2023/2/7  16:34
 */
@Service
public class GroupManagementDetailServiceImpl extends ServiceImpl<GroupManagementDetailMapper, GroupManagementDetail> implements GroupManagementDetailService {

    @Resource
    private GroupManagementDetailMapper groupManagementDetailMapper;

    @Resource
    private SchedulingPlanFeignService schedulingPlanFeignService;

    /**
     * 班组管理明细列表查询
     *
     * @param id
     * @return
     */
    @Override
    public List<GroupManagementDetailViewDto> getList(Long id) {

        //通过id查出班组明细列表
        List<GroupManagementDetailDto> groupManagementDetailViewDto = groupManagementDetailMapper.selectDeatilList(id);

        List<SysDictData> statusArray = DictUtils.getDictCache("scheduling_class_info");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //遍历班组明细获取日历id和班别
        List<GroupManagementDetailViewDto> list = new ArrayList<>();
        for (GroupManagementDetailDto groupManagementDetail : groupManagementDetailViewDto) {
            //返回列表
            GroupVo groupVo = new GroupVo();
            groupVo.setCalendarId(groupManagementDetail.getGroupCalendarId());
            groupVo.setTeamId(groupManagementDetail.getGroupCalendarTeam());
            GroupManagementDetailViewDto groupManagementDetailViewDto1 = groupManagementDetailMapper.selectCalendar(groupVo);
            if (groupManagementDetailViewDto1 != null) {
                groupManagementDetailViewDto1.setLine(groupManagementDetail.getLine());
                groupManagementDetailViewDto1.setLineCode(groupManagementDetail.getLineCode());
                groupManagementDetailViewDto1.setShiftDict(statusMap.get(groupManagementDetailViewDto1.getShift() + ""));
                groupManagementDetailViewDto1.setShift(String.valueOf(groupManagementDetail.getGroupCalendarTeam()));
                groupManagementDetailViewDto1.setIsStart(groupManagementDetail.getIsStart());
            }
            if (groupManagementDetailViewDto1!=null){
                list.add(groupManagementDetailViewDto1);
            }

        }
        return list;
    }

    /**
     * 是否启用
     *
     * @param groupManagementDetailVo
     * @return
     */
    @Override
    public boolean isStart(GroupManagementDetailVo groupManagementDetailVo) {
        GroupManagementDetail groupManagementDetail = groupManagementDetailMapper.selectById(groupManagementDetailVo.getId());
        groupManagementDetail.setIsStart(groupManagementDetailVo.getIsStart());

        return updateById(groupManagementDetail);
    }
}
